<!--
~ Licensed to the Apache Software Foundation (ASF) under one or more
~ contributor license agreements.  See the NOTICE file distributed with
~ this work for additional information regarding copyright ownership.
~ The ASF licenses this file to You under the Apache License, Version 2.0
~ (the "License"); you may not use this file except in compliance with
~ the License.  You may obtain a copy of the License at
~
~    http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
~
-->

<div class="tree-input-header" fxLayoutAlign="start center">
    <div fxLayout="row" fxFlex="100">
        <div fxFlex fxLayoutAlign="start center"><b>Browse</b></div>
        <div fxLayoutAlign="end center">
            <button
                mat-icon-button
                color="accent"
                (click)="largeView = !largeView"
            >
                <mat-icon *ngIf="!largeView">open_in_full</mat-icon>
                <mat-icon *ngIf="largeView">close_fullscreen </mat-icon>
            </button>
        </div>
    </div>
</div>

<mat-tree
    [dataSource]="dataSource"
    [treeControl]="treeControl"
    #tree
    class="sp-tree"
    [ngClass]="largeView ? 'tree-large-height' : 'tree-normal-height'"
>
    <mat-tree-node *matTreeNodeDef="let node" matTreeNodeToggle>
        <div
            fxLayoutAlign="start center"
            [attr.data-cy]="'tree-node-' + node.nodeName"
        >
            <i class="material-icons color-primary pr-5">wifi</i>
            <i
                class="material-icons pr-5 icon-button"
                *ngIf="node.dataNode && !isNodeSelected(node)"
                (click)="addNode(node)"
                [attr.data-cy]="'select-' + node.nodeName"
                matTooltip="Add node"
                >add_circle</i
            >
            <i
                class="material-icons pr-5 icon-button"
                *ngIf="node.dataNode && isNodeSelected(node)"
                (click)="removeNode(node)"
                matTooltip="Remove node"
                >remove_circle</i
            >
            <i
                class="material-icons pr-5 icon-button"
                *ngIf="selectedNodeId !== node.internalNodeName"
                [attr.data-cy]="'show-node-details-' + node.nodeName"
                (click)="showNodeDetails(node)"
                matTooltip="Show details"
                >visibility</i
            >
            <i
                class="material-icons pr-5 icon-button"
                *ngIf="selectedNodeId === node.internalNodeName"
                [attr.data-cy]="'hide-node-details-' + node.nodeName"
                (click)="hideNodeDetails()"
                matTooltip="Show details"
                >visibility_off</i
            >
            <span class="pr-5">{{ node.nodeName }}</span>
        </div>
    </mat-tree-node>
    <mat-nested-tree-node *matTreeNodeDef="let node; when: hasChild">
        <div class="mat-tree-node">
            <button
                mat-icon-button
                matTreeNodeToggle
                [attr.data-cy]="'expand-' + node.nodeName"
                [attr.aria-label]="'Toggle ' + node.nodeName"
                (click)="loadChildren(node, treeControl.isExpanded(node))"
            >
                <mat-icon class="mat-icon-rtl-mirror">
                    {{
                        treeControl.isExpanded(node)
                            ? 'expand_more'
                            : 'chevron_right'
                    }}
                </mat-icon>
            </button>
            <div fxLayoutAlign="start center">
                <i
                    class="material-icons color-primary pr-5"
                    *ngIf="!treeControl.isExpanded(node)"
                    >folder</i
                >
                <i
                    class="material-icons color-primary pr-5"
                    *ngIf="treeControl.isExpanded(node)"
                    >folder_open</i
                >
                <i
                    class="material-icons pr-5 icon-button"
                    *ngIf="
                        treeControl.isExpanded(node) && hasDataChildren(node)
                    "
                    matTooltip="Add all direct children"
                    (click)="addAllDirectChildren(node)"
                    >add_circle</i
                >
                <i
                    class="material-icons pr-5 icon-button"
                    *ngIf="selectedNodeId !== node.internalNodeName"
                    [attr.data-cy]="'show-node-details-' + node.nodeName"
                    (click)="showNodeDetails(node)"
                    matTooltip="Show details"
                    >visibility</i
                >
                <i
                    class="material-icons pr-5 icon-button"
                    *ngIf="selectedNodeId === node.internalNodeName"
                    [attr.data-cy]="'hide-node-details-' + node.nodeName"
                    (click)="hideNodeDetails()"
                    matTooltip="Show details"
                    >visibility_off</i
                >
                <span class="node-name pr-5">&nbsp;{{ node.nodeName }}</span>
            </div>
        </div>
        <div *ngIf="treeControl.isExpanded(node)" role="group">
            <ng-container matTreeNodeOutlet></ng-container>
        </div>
    </mat-nested-tree-node>
</mat-tree>
